package com.yuncool.controller.v1;

import com.yuncool.ApiMessage;
import com.yuncool.ServiceResult;
import com.yuncool.constants.ResultCode;
import com.yuncool.domain.vo.RegisteredRequestVO;
import com.yuncool.service.CommonService;
import com.yuncool.service.MemberInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("auth")
@Slf4j
@Api(tags = "用户权限系统", value = "此接口任意方法无需登陆即可访问!")
public class AuthController {

	@Autowired
	MemberInfoService memberInfoService;
	@Autowired
	CommonService commonService;

	@ApiOperation(value = "注册用户接口", httpMethod = "POST")
	@PostMapping("registeredUser")
	public ServiceResult<Boolean> registeredUser(@RequestBody RegisteredRequestVO vo) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			result = memberInfoService.registeredUser(vo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "校验用户是否已注册接口", httpMethod = "POST")
	@PostMapping("checkUserExist")
	public ServiceResult<Boolean> checkUserExist(String loginId) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			result = memberInfoService.checkUserExist(loginId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}


	@ApiOperation(value = "校验验证码接口", httpMethod = "POST")
	@PostMapping("checkCode")
	public ServiceResult<Boolean> checkCode(@RequestParam String loginId, @RequestParam String code) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		result.setResultData(true);
		try {
			Boolean aBoolean = commonService.checkSmsCode(loginId, code);
			if (!aBoolean) {
				result.setError(ResultCode.REQUEST_ERROR_NODATA, "验证码错误!");
				result.setResultData(false);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}
}
